﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using Seagull.BarTender.Print;
using System.Windows.Forms;

namespace POSPlus.PrintManager
{
	public abstract class EzbtWriter
	{
		public abstract string Target { get; set; }
		public abstract List<PrintJob> JobName { get; set; }
		public abstract string ConnectionString { get; set; }

		public abstract void Write();
		public abstract void GetInfo();
		
		public string CircleFile(int vendor)
		{
			switch (vendor)
			{
				case 1:
                case 351: return "reddot.bmp";
                case 3: return "yellowdot.bmp";
                case 56: return "bluedot.bmp";
                default: return "blank.bmp";
			}
		}

		public string AvgDailyMovement(double movement)
		{
			return String.Format("{0:0.0}", Math.Round(movement, 1));
		}

		public string UnitNP(double unit)
		{
			string result = "";

			result = string.Format("{0:0.000}", Math.Round(unit, 3));
			result = "$" + result;

			return result;
		}

		public string RegularUpcCheckbit(int vendor, string cert_code)
		{
			if (vendor == 1 || vendor == 351)
				return string.Format("{0}", cert_code);
			else
				return "";
		}

		public string RegularUpcNoCheckbit(int vendor, string certCode)
		{
			if (vendor == 3)
				return string.Format("{0}", certCode);
			else
				return "";
		}

		public string RegularKehe(int vendor, string upc)
		{
			if (vendor == 56)
				return string.Format("{0}", upc.Substring(3, 10));
			else
				return "";
		}

		public string Authorized(int hosted)
		{
			if (hosted == 1)
				return "";
			else
				return "UNAUTHORIZED";
		}

        
		public string PriceLine1(int priceMethod, decimal price, int quantity, decimal groupPrice)
		{
			switch (priceMethod)
			{
				case 0:
				case 1: return "";
				case 2: return string.Format("Buy {0} At {1}{2}", quantity - 1, ConvertMoney(price), Environment.NewLine);
				case 3: return string.Format("Buy One At {0}{1}", ConvertMoney(price), Environment.NewLine);
				case 4: return string.Format("{0} Limit {1}{2}", ConvertMoney(groupPrice), quantity, Environment.NewLine);
				default: return "";
			}
		}

		public string PriceLine1(int priceMethod, decimal price, double quantity, decimal groupPrice)
		{
			switch (priceMethod)
			{
				case 0:
				case 1: return "";
				case 2: return string.Format("Buy {0} At {1}{2}", quantity - 1, ConvertMoney(price), Environment.NewLine);
				case 3: return string.Format("Buy One At {0}{1}", ConvertMoney(price), Environment.NewLine);
				case 4: return string.Format("{0} Limit {1}{2}", ConvertMoney(groupPrice), quantity, Environment.NewLine);
				default: return "";
			}
		}

		public string PriceLine2(int priceMethod, decimal price, int quanity, decimal groupPrice)
		{
			switch (priceMethod)
			{
				case 0: return string.Format("{0}", ConvertMoney(price));
				case 1: return string.Format("{0}/{1}", quanity, ConvertMoney(groupPrice));
				case 2:
				case 3:
				case 4:
				default: return "";
			}
		}

		public string PriceLine2(int priceMethod, decimal price, double quantity, decimal groupPrice)
		{
			switch (priceMethod)
			{
				case 0: return string.Format("{0}", ConvertMoney(price));
				case 1: return string.Format("{0}/{1}", quantity, ConvertMoney(groupPrice));
				case 2:
				case 3:
				case 4:
				default: return "";
			}
		}

		public string PriceLine3(int priceMethod, decimal price, int quantity, decimal groupPrice)
		{
			switch (priceMethod)
			{
				case 0:
				case 1: return "";
				case 2:
					if (groupPrice == (price * (quantity - 1)))
						return "Get One Free!";
					else
						return string.Format("Get Next For {0}", ConvertMoney(groupPrice - (price * (quantity - 1))));
				case 3: return string.Format("Buy Quantity For {0}", ConvertMoney(groupPrice));
				case 4: return string.Format("Additional For {0}", ConvertMoney(price));
				default: return "";
			}
		}

		public string PriceLine3(int priceMethod, decimal price, double quantity, decimal groupPrice)
		{
			switch (priceMethod)
			{
				case 0:
				case 1: return "";
				case 2:
					if ((double)groupPrice == ((double)price * (quantity - 1)))
						return "Get One Free!";
					else
						return string.Format("Get Next For {0}", ConvertMoney((double)groupPrice - ((double)price * (quantity - 1))));
				case 3: return string.Format("Buy Quantity For {0}", ConvertMoney(groupPrice));
				case 4: return string.Format("Additional For {0}", ConvertMoney(price));
				default: return "";
			}
		}

		public string ConvertMoney(decimal amount)
		{
			decimal value = Math.Round(amount, 2, MidpointRounding.AwayFromZero);
			if (value < 1)
			{
				value = value * 100;
				return value.ToString("0") + "\u00A2";
			}
			else if (value == 1)
				return string.Format("${0}", value);
			else
				return string.Format("{0:0.00}", value);
		}

		public string ConvertMoney(double amount)
		{
			double value = Math.Round(amount, 2, MidpointRounding.AwayFromZero);

			if (value < 1)
			{
				value = value * 100;
				return value.ToString() + "\u00A2";
			}
			else if (value - Math.Floor(value) == 0)
				return string.Format("${0}", value);
			else
				return string.Format("{0:0.00}", value);
		}

        protected void Print()
        {
            using (Engine engine = new Engine(true))
            {
                Messages msgs = null;
                engine.XMLScript(Target, XMLSourceType.ScriptFile, out msgs);
 
                foreach (Seagull.BarTender.Print.Message msg in msgs)
                    MessageBox.Show(msg.Text, msg.Source.ToString());

                engine.Stop();
            }
        }
	}
}
